package com.ruoyi.pcwl.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.ruoyi.pcwl.domain.vo.ReplayVo;
import com.ruoyi.pcwl.domain.vo.SubjectVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.pcwl.domain.query.ReplayQuery;
import com.ruoyi.pcwl.domain.Replay;
import com.ruoyi.pcwl.mapper.ReplayMapper;
import com.ruoyi.pcwl.service.ReplayService;


/**
 * 评论信息表 业务接口实现
 */
@Service("replayService")
public class ReplayServiceImpl extends ServiceImpl<ReplayMapper, Replay> implements ReplayService {


	/**
	 * 据条件查询记录数
	 */
	@Override
	public Integer findCountByParam(ReplayQuery reqParam) {
		QueryWrapper<Replay> wrapper = new QueryWrapper<>();
		if (reqParam.getId()!=null)
			wrapper.eq("id",reqParam.getId());
		if (reqParam.getParentId()!=null)
			wrapper.eq("parent_id",reqParam.getParentId());
		if (StringUtils.isNotEmpty(reqParam.getUserId())) {
			wrapper.like("user_id", reqParam.getUserId());
		}
		if (StringUtils.isNotEmpty(reqParam.getNickName())) {
			wrapper.like("nick_name", reqParam.getNickName());
		}
		if (StringUtils.isNotEmpty(reqParam.getUserImage())) {
			wrapper.like("user_image", reqParam.getUserImage());
		}
		if (StringUtils.isNotEmpty(reqParam.getSubjectId())) {
			wrapper.like("subject_id", reqParam.getSubjectId());
		}
		if (StringUtils.isNotEmpty(reqParam.getContent())) {
			wrapper.like("content", reqParam.getContent());
		}
		if (reqParam.getCategoryId()!=null)
			wrapper.eq("category_id",reqParam.getCategoryId());
		if (StringUtils.isNotEmpty(reqParam.getCategoryName())) {
			wrapper.like("category_name", reqParam.getCategoryName());
		}
		Long aLong = baseMapper.selectCount(wrapper);
		return aLong.intValue();
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public IPage<Replay> selectPage(ReplayQuery reqParam) {
		QueryWrapper<Replay> wrapper = new QueryWrapper<>();
		if (reqParam.getId()!=null)
			wrapper.eq("id",reqParam.getId());
		if (reqParam.getParentId()!=null)
			wrapper.eq("parent_id",reqParam.getParentId());
		if (StringUtils.isNotEmpty(reqParam.getUserId())) {
			wrapper.like("user_id", reqParam.getUserId());
		}
		if (StringUtils.isNotEmpty(reqParam.getNickName())) {
			wrapper.like("nick_name", reqParam.getNickName());
		}
		if (StringUtils.isNotEmpty(reqParam.getUserImage())) {
			wrapper.like("user_image", reqParam.getUserImage());
		}
		if (StringUtils.isNotEmpty(reqParam.getSubjectId())) {
			wrapper.like("subject_id", reqParam.getSubjectId());
		}
		if (StringUtils.isNotEmpty(reqParam.getContent())) {
			wrapper.like("content", reqParam.getContent());
		}
		if (reqParam.getCategoryId()!=null)
			wrapper.eq("category_id",reqParam.getCategoryId());
		if (StringUtils.isNotEmpty(reqParam.getCategoryName())) {
			wrapper.like("category_name", reqParam.getCategoryName());
		}
		return baseMapper.selectPage(reqParam.getPage(), wrapper);
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(Replay bean) {
		return baseMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<Replay> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return baseMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
//	@Override
//	public Integer addOrUpdateBatch(List<Replay> listBean) {
//		if (listBean == null || listBean.isEmpty()) {
//			return 0;
//		}
//		return baseMapper.insertOrUpdateBatch(listBean);
//	}

	/**
	 * 多条件更新
	 */
//	@Override
//	public Integer updateByParam(Replay bean, ReplayQuery param) {
//		return baseMapper.updateByParam(bean, param);
//	}

	/**
	 * 多条件删除
	 */
//	@Override
//	public Integer deleteByParam(ReplayQuery param) {
//		return baseMapper.deleteByParam(param);
//	}

	/**
	 * 根据Id获取对象
	 */
	@Override
	public Replay getReplayById(Long id) {
		return baseMapper.selectById(id);
	}

	/**
	 * 根据Id修改
	 */
	@Override
	public Integer updateReplayById(Replay bean) {
		return baseMapper.updateById(bean);
	}

	/**
	 * 根据Id删除
	 */
	@Override
	public Integer deleteReplayById(Long id) {


			List<Replay> replayList = baseMapper.selectList(new QueryWrapper<Replay>().eq("parent_id", id));
			if (CollectionUtils.isNotEmpty(replayList)) {
				for (Replay replay1 : replayList) {
					//递归删除
					this.deleteReplayById(replay1.getId());
					baseMapper.deleteById(replay1.getId());
				}
			}

		return baseMapper.deleteById(id);
	}

	/**
	 * 根据主题id查询评论
	 * @param subjectId
	 * @return
	 */
	@Override
	public SubjectVo selectReplayBySubjectId(Long subjectId) {
		List<Replay> replayList = baseMapper.selectList(new QueryWrapper<Replay>().eq("subject_id", subjectId));

		SubjectVo subjectVo = new SubjectVo();

		for (Replay replay : replayList) {
			if (replay.getParentId()==-1) {
				ReplayVo replayVo = new ReplayVo();
				BeanUtils.copyProperties(replay, replayVo);
				subjectVo.addReplay(replayVo);
			}
		}
		for (ReplayVo replayVo : subjectVo.getReplayVoList()) {
			this.getRepalyByParentId(replayList, replayVo);
		}
		return subjectVo;
	}

	/**
	 * 递归方法收集所有评论
	 */
	private void getRepalyByParentId(List<Replay> replayList, ReplayVo replayVo) {
		for (Replay replay : replayList) {
			if (replay.getParentId().equals(replayVo.getId())) {
				ReplayVo replayVo1 = new ReplayVo();
				BeanUtils.copyProperties(replay, replayVo1);
				replayVo.addReplay(replayVo1);
				//递归
				this.getRepalyByParentId(replayList, replayVo1);
			}
		}
	}
}
